#!/bin/sh
cd ${0%/*} || exit 1    # Run from this directory

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

runApplication blockMesh

#- Test writing collated format
runApplication decomposePar -fileHandler collated
runParallel foamRun -fileHandler collated
runApplication reconstructPar -latestTime -fileHandler collated

#- Test writing uncollated format
runApplication -s uncollated decomposePar -fileHandler uncollated -force
runParallel -s uncollated foamRun -fileHandler uncollated


#- Test uncollated+distributed running: copy to different roots
rm -rf machineA/fileHandler
mkdir -p machineA/fileHandler
(   cd machineA/fileHandler && \
    cp -R ../../processor[0-1] . && \
    cp -R ../../system . && \
    mkdir -p constant && find ../../constant -type f -exec cp {} constant/ \;
)
#- Note: slave node does not need constant&system since these are global
rm -rf machineB/fileHandler
mkdir -p machineB/fileHandler
(   cd machineB/fileHandler && \
    cp -R ../../processor[2-3] .
)
#- Run with different roots
(   d=$PWD && \
    cd machineA/fileHandler && \
    runParallel -s multiRoot foamRun \
    -fileHandler masterUncollated -ioRanks '(0 2)' \
    -roots "(\"$d/machineA\" \"$d/machineB\" \"$d/machineB\")"
)


#- Restart from uncollated
runParallel -s collated foamRun -fileHandler collated
runApplication -s collated reconstructPar -latestTime -fileHandler collated

#- Convert the parallel format to uncollated
runParallel foamFormatConvert -fileHandler uncollated

#- Restart with multiple IO ranks
runParallel -s multiCollated \
    foamRun -fileHandler collated -ioRanks '(0 2)'

#- Reconstruct the multi-rank format. Delete the collated directory
#  since conflicts with the multi-collated directory
rm -rf processors4
runApplication -s multiCollated reconstructPar -latestTime \
    -fileHandler collated -ioRanks '(0 2)'

#- Convert the multi-rank format to uncollated
runParallel -s uncollated foamFormatConvert -fileHandler uncollated

#- Restart from multiCollated using collated
runParallel -s uncollated_from_multiCollated \
    foamRun -fileHandler uncollated



#- Test collated+distributed running: copy to different roots
#  Important: make sure to copy uniform since we're copying it

#- Delete all processor directories
runApplication -s collated decomposePar \
    -fileHandler collated -force -copyUniform

rm -rf machineA/fileHandler
mkdir -p machineA/fileHandler
(   cd machineA/fileHandler && \
    cp -R ../../processor* . && \
    cp -R ../../system . && \
    mkdir -p constant && find ../../constant -type f -exec cp {} constant/ \;
)
#- Note: slave node does not need constant&system since these are global
rm -rf machineB/fileHandler
mkdir -p machineB/fileHandler

#- Run with different roots
(   d=$PWD && \
    cd machineA/fileHandler && \
    runParallel -s distributed_multiCollated foamRun \
    -fileHandler collated -ioRanks '(0 2)' \
    -roots "(\"$d/machineA\" \"$d/machineB\" \"$d/machineB\")"
)


#------------------------------------------------------------------------------
